using System;
using Habanero.Base;
using Habanero.Base.Exceptions;

namespace FireStarterModeller.BO
{
    public class DMLookupListSimpleMustHaveItemRule : IBusinessObjectRule
    {
        private DMLookupListSimple _dmLookupListSimple;

//        public DMLookupListSimpleMustHaveItemRule(DMLookupListSimple dmLookupListSimple)
//        {
//            if (dmLookupListSimple == null) throw new ArgumentNullException("dmLookupListSimple");
//            _dmLookupListSimple = dmLookupListSimple;
//        }

        #region Implementation of IBusinessObjectRule

        /// <summary>
        /// Indicates whether the property value is valid against the rules
        /// </summary>
        /// <returns>
        /// Returns true if valid
        /// </returns>
        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _dmLookupListSimple = (DMLookupListSimple) bo;
            return _dmLookupListSimple.ListItems.Count > 0;
        }

        /// <summary>
        /// Returns the rule name
        /// </summary>
        public string Name
        {
            get { return "DMLookupListSimpleMustHaveItem"; }
        }

        /// <summary>
        /// Returns the error message for if the rule fails.
        /// </summary>
        public string Message
        {
            get
            {
                DMProperty dmProperty = _dmLookupListSimple.DMProperty;
                if (dmProperty == null)
                {
                    return "The Lookup List which has no related Property must have at least one item.";
                }
                if (dmProperty.Class == null)
                {
                    return "The Lookup List for the Property '" + dmProperty.ToString() + "' with no related Class must have at least one item.";
                }
                return "The Lookup List for the Property '" + dmProperty.ToString() + "' on Class '" + dmProperty.Class.ToString() + "' must have at least one item.";
            }
        }
        /// <summary>
        /// The <see cref="P:Habanero.Base.IBusinessObjectRule.ErrorLevel" /> for this BusinessObjectRule e.g. Warning, Error. 
        /// </summary>
        public ErrorLevel ErrorLevel
        {
            get { return Habanero.Base.ErrorLevel.Error; }
        }
        #endregion
    }
}